#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <errno.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <string.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>

unsigned char buf[] = {
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x1c, 0x25, 0x6a, 0x90, 0xe3, 0x88, 0x63, 0x11, 0x09,
    0x00, 0x00, 0x00, 0x10, 0x01, 0x01, 0x00, 0x00, 0x01, 0x03, 0x00, 0x08, 0x05, 0x00, 0x00, 0x00,
    0x06, 0x00, 0x00, 0x00
};

unsigned char buf_2[] = {
    0x08, 0x00, 0x3e, 0x26, 0x0a, 0x5b, 0x00, 0x16, 0xd3, 0x93, 0xe4, 0x91, 0x88, 0x63, 0x11, 0x19,
    0x00, 0x00, 0x00, 0x24, 0x01, 0x01, 0x00, 0x00, 0x01, 0x03, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00,
    0x02, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x10, 0x19, 0xc9, 0xd3, 0x85, 0x12, 0x34, 0x91, 0xbd,
    0x3b, 0x2a, 0xb4, 0x09, 0xa9, 0x5f, 0x8d, 0xe2
};

int main(int argc, char *argv[])
{
    int sockfd;
    int ret;
    struct sockaddr_ll sll;
    struct ifreq ifstruct;
    struct sockaddr_in dst;
    
    sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    if (sockfd < 0)
        perror("sockfd < 0 \n");

    memset(&sll, 0, sizeof(sll));
    sll.sll_family = AF_PACKET;
    sll.sll_family = PF_PACKET;
    sll.sll_halen = 6;

    

    strcpy(ifstruct.ifr_name, "eth0");
    if (ioctl(sockfd, SIOCGIFINDEX, &ifstruct) != 0)
        perror("ioctl fail\n");

    printf("index = %d\n", ifstruct.ifr_ifindex);
    
    sll.sll_ifindex = ifstruct.ifr_ifindex;
    sll.sll_protocol = htons(0x8083);

    if(bind(sockfd, (struct sockaddr *) &sll, sizeof(sll)) == -1) {    
        perror("bind fail\n");
    }

    memset(&dst, 0, sizeof(dst));
    dst.sin_family = AF_INET;
    
    ret = sendto(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)&sll, sizeof(sll));
    printf("ret = %d, err = %d\n", ret, errno);

    sleep(1);

    ret = sendto(sockfd, buf_2, sizeof(buf_2), 0, (struct sockaddr *)&sll, sizeof(sll));
    printf("ret = %d, err = %d\n", ret, errno);
    return (0);
}
